<template>
  <div id="tag">
    <el-tag
      v-for="(tag, index) in tags"
      :key="tag.name"
      :closable="tag.name !== 'home'"
      :effect="$route.path === tag.path ? 'dark' : 'plain'"
      @click="handleTag(tag)"
      @close="closeTag(tag, index)"
    >
      {{tag.label}}
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
export default {
  name: 'Tag',
  computed: {
    ...mapState({
      tags: state => state.tab.tabList
    })
  },
  methods: {
    ...mapMutations({
      close: 'closeTag'
    }),
    handleTag(tag) {
      this.$router.push({name: tag.name})
    },
    closeTag(tag, index) {
      //获取tags长度
      const length = this.tags.length
      this.close(tag)
      // 当前路由等于活跃的tag
      if(this.$route.name !== tag.name) {
        return;
      }
      // 活跃tag在最右边
      if(index === length -1) {
        this.$router.push({
          name: this.tags[index -1].name
        })
      }else {
        //活跃tag在中间, 向右移动
        this.$router.push({
          name: this.tags[index].name
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
#tag {
  padding: 15px;
  .el-tag {
    margin-right: 10px;
    cursor: pointer;
  }
}
</style>